/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.sql.*;

/**
 *
 * @author Administrator
 */
public class DAOQuery extends BaseDAO {

    private Connection con = getConnection();
    private ResultSet rs = null;
    public DAOQuery() {
    }

    public ResultSet selectQuestion(int id) {
        try {
            
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT TOP 10 * FROM QUESTIONS WHERE subID = " + id + "ORDER BY NEWID()");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;
    }

    public ResultSet selectAnswer(int id) {
        try {
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ANSWERS WHERE QUESTIONID = " + id + "ORDER BY NEWID()");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;
    }
    public ResultSet selectAnswerLast(int id) {
        try {
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ANSWERS WHERE QUESTIONID = " + id);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;
    }
    public ResultSet selectQuestionLast(int id,int QuestionID) {
        try {
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM QUESTIONS WHERE subID = " + id+" AND QuestionID = "+QuestionID);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;
    }

    public ResultSet checkTest(int id, String idAnswer) {
        try {
            //SELECT * FROM ANSWERS WHERE QUESTIONID = 1 AND TRUEORFALSE = 1
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ANSWERS WHERE QUESTIONID = "+id+"AND ANSWERID in ("+idAnswer+")");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;
    }

    public ResultSet calculatorMark(String id) {
        try {
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM QUESTIONS WHERE QUESTIONID in (" + id+(" )"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    public int counterRowTrue(int id) {
        int number = 0;
        try {
            //SELECT * FROM ANSWERS WHERE QUESTIONID = 1 AND TRUEORFALSE = 1
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ANSWERS WHERE QUESTIONID = " + id + " AND TrueOrFalse = 1");
            while (rs.next()) {
                number++;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return number;
    }
    public int counterRowFalse(int id) {
        int number = 0;
        try {
            //SELECT * FROM ANSWERS WHERE QUESTIONID = 1 AND TRUEORFALSE = 1
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ANSWERS WHERE QUESTIONID = " + id + " AND TrueOrFalse = 0");
            while (rs.next()) {
                number++;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return number;
    }

    public int counterCheckTrue(int id, String idAnswer) {
        int number = 0;
        try {

            //SELECT * FROM ANSWERS WHERE QUESTIONID = 1 AND TRUEORFALSE = 1
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ANSWERS WHERE QUESTIONID = " + id + "AND ANSWERID in (" + idAnswer + ") AND TrueOrFalse = 1");
            while (rs.next()) {
                number++;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return number;
    }
    public int counterCheckFalse(int id, String idAnswer) {
        int number = 0;
        try {

            //SELECT * FROM ANSWERS WHERE QUESTIONID = 1 AND TRUEORFALSE = 1
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ANSWERS WHERE QUESTIONID = " + id + "AND ANSWERID in (" + idAnswer + ") AND TrueOrFalse = 0");
            while (rs.next()) {
                number++;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return number;
    }
     public ResultSet selectAnswerTrue(int idQuestion) {
        ResultSet rs = null;
        try {

            //SELECT * FROM ANSWERS WHERE QUESTIONID = 1 AND TRUEORFALSE = 1
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ANSWERS where QuestionID = " + idQuestion + " and TrueOrFalse = 1");

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;
    }
    
}
